package com.daniel.memo;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;

import com.daniel.database.Database;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MemoViewActivity extends Activity {
	
	private Database db;	
	private EditText getFileNameEditTxt;
	private Dialog editTileDiaglog;
	
	private TextView titleText;
	private TextView bodyText;
	
	private int memoRowId = -1;
	
	private String newFileName;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.memo_view_activity);
        db = new Database(this);

        initTextView();

        Bundle extras = getIntent().getExtras();   
        if (extras != null) {
        	memoRowId = extras.getInt(Memo.MEMO_ID);
        	Log.d("memoRowId ","" + memoRowId);
        }
    }

    @Override
    public void onStart() {
    	super.onStart();
    	db.open();
    	if (memoRowId != -1) {
    		pupulateData(memoRowId);
    	}
    }

    @Override
    public void onStop() {
    	super.onStop();
    	db.close();
    }
        
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.memo_view_memu, menu);
        return true;
    }
    
    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
    	switch(item.getItemId()) {
	    	case R.id.memo_view_menu_delete_id:
	    		db.deleteByMemoId(memoRowId);
	    		finish();
	    		return true;
	    		
	    	case R.id.memo_view_menu_export_id:
	    		
	    		exportMemoToFile();
	    		return true;
	    	
	    	case R.id.memo_view_menu_edit_id:
	    		startMemoEditActivity();
	    		return true;
	    		
    	}
    	return super.onMenuItemSelected(featureId, item);
    }
    
    
    public void initTextView() {
    	titleText = (TextView) findViewById(R.id.title_txt_id);
    	bodyText = (TextView) findViewById(R.id.body_txt_id);
    	
    }

    
    public void pupulateData(int memoRowId) {
    	Cursor memoCursor = db.fetchMemoById(memoRowId);
    	
    	memoCursor.moveToFirst();
    	int numOfRow = memoCursor.getCount();
    	
    	if (numOfRow > 0) {
    		titleText.setText(memoCursor.getString(0));
        	bodyText.setText(memoCursor.getString(2));
    	}
    }
    
    public void startMemoEditActivity() {
    	Intent memoEditIntent = new Intent(this , MemoEditActivity.class);
    	memoEditIntent.putExtra(Memo.MEMO_ID, memoRowId);
    	startActivity(memoEditIntent);
    	
    }
    
    public void exportMemoToFile() {

    	//get directory of sdCard.
    	File sdDir = Environment.getExternalStorageDirectory();
    	final File dir = new File(sdDir.getAbsoluteFile() + "/Memo");
    	dir.mkdir();

    	Button getFileNameBtn;
    	editTileDiaglog = new Dialog(this);
    	editTileDiaglog.setTitle(R.string.edit_memo_file_name);
    	editTileDiaglog.setContentView(R.layout.edit_file_dialog);

    	getFileNameEditTxt = (EditText) editTileDiaglog.findViewById(R.id.edit_fileName_edtTxt_id);
    	
    	getFileNameBtn = (Button) editTileDiaglog.findViewById(R.id.confiem_fileName_btn_id);
    	getFileNameBtn.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				String fileName = getFileNameFromDialog();				
				File file = new File(dir, "" + fileName + ".txt");
				saveFile(file);
				editTileDiaglog.dismiss();
				Toast.makeText(getApplicationContext(), "Memo exported to file", Toast.LENGTH_SHORT).show();
			}
		});
    	
    	File file = new File(dir, "" + titleText.getText().toString() + ".txt");
    	
    	if (file.exists()) {
    		getFileNameEditTxt.setText(titleText.getText().toString());
    		editTileDiaglog.show();
    		
    		Toast.makeText(getApplicationContext(), "File already exist. Please change the file name.", Toast.LENGTH_LONG).show();
    		return;
    	}
    	
    	saveFile(file);
    	Toast.makeText(getApplicationContext(), "Memo exported to file", Toast.LENGTH_SHORT).show();
    }
    
    private void saveFile(File file) {
		String s = "";
		s += titleText.getText().toString();
		s += "|";
		s += "Memo : " + bodyText.getText().toString();
		
		try {
			FileOutputStream fos = new FileOutputStream(file);
			fos.write(s.getBytes());
	        fos.close();
	        
		} catch (IOException e) {
			Log.e("Export file error " , "IOException " + e.toString());
			
		}
    }
    
    private String getFileNameFromDialog() {
    	String result;
    	result = getFileNameEditTxt.getText().toString();
    	
    	return result;
    }
    
}
